﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using BUL;
using System.Data;
namespace Hospital
{
    public partial class ManagerDrug : System.Web.UI.Page
    {
        DrugDAO drug = new DrugDAO();
        DrugBUL bul = new DrugBUL();
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            grvDrug.DataSource = bul.load();
            grvDrug.DataBind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (txtId.Text.Equals(""))
            {
                btnDelete.Text = "aaa";
                drug.Name = txtName.Text;
                drug.Unit = txtUnit.Text;
                drug.Desription = txtDescription.Text;
                drug.Amount = Int32.Parse(txtAmount.Text);
                bul.insert(drug);
                Page_Load(sender, e);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if(!txtId.Text.Equals("")){
            bul.delete(txtId.Text);
            Page_Load(sender, e);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!txtId.Text.Equals(""))
            {
                drug.IdDrug =Int32.Parse(txtId.Text);
                drug.Name = txtName.Text;
                drug.Unit = txtUnit.Text;
                drug.Desription = txtDescription.Text;
                drug.Amount = Int32.Parse(txtAmount.Text);
                bul.update(drug);
                Page_Load(sender, e);
            }
        }

        protected void txtReset_Click(object sender, EventArgs e)
        {
            txtId.Text = "";
            txtName.Text = "";
            txtUnit.Text = "";
            txtDescription.Text = "";
            txtAmount.Text = "";
        }

        protected void grvDrug_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtId.Text =HttpUtility.HtmlDecode( grvDrug.SelectedRow.Cells[1].Text);
            txtName.Text =HttpUtility.HtmlDecode( grvDrug.SelectedRow.Cells[2].Text);
            txtUnit.Text =HttpUtility.HtmlDecode( grvDrug.SelectedRow.Cells[4].Text);
            txtDescription.Text =HttpUtility.HtmlDecode( grvDrug.SelectedRow.Cells[3].Text);
            txtAmount.Text =HttpUtility.HtmlDecode( grvDrug.SelectedRow.Cells[5].Text);
        }
    }
}